package edu.scau.DAO_mode.ui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JPasswordField;

import edu.scau.DAO_mode.model.User;
import edu.scau.DAO_mode.serviceimp.UserServiceImp;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RegisterDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField usernameTextField;
	private JPasswordField passwordField;
	private JPasswordField rePasswordField;

	/**
	 * Launch the application.
	 * 
	 * public static void main(String[] args) { try { RegisterDialog dialog =
	 * new RegisterDialog();
	 * dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	 * dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
	 */

	/**
	 * Create the dialog.
	 */
	public RegisterDialog() {
		setTitle("注册");
		setBounds(100, 100, 325, 292);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel label = new JLabel("用户名");
			label.setFont(new Font("宋体", Font.PLAIN, 20));
			label.setBounds(40, 34, 81, 46);
			contentPanel.add(label);
		}
		{
			usernameTextField = new JTextField();
			usernameTextField.setColumns(10);
			usernameTextField.setBounds(131, 49, 109, 21);
			contentPanel.add(usernameTextField);
		}
		{
			passwordField = new JPasswordField();
			passwordField.setBounds(131, 90, 109, 21);
			contentPanel.add(passwordField);
		}
		{
			JLabel label = new JLabel("密码");
			label.setFont(new Font("宋体", Font.PLAIN, 20));
			label.setBounds(40, 75, 81, 46);
			contentPanel.add(label);
		}
		
		JLabel label = new JLabel("确认密码");
		label.setFont(new Font("宋体", Font.PLAIN, 20));
		label.setBounds(40, 131, 81, 46);
		contentPanel.add(label);
		
		rePasswordField = new JPasswordField();
		rePasswordField.setBounds(131, 146, 109, 21);
		contentPanel.add(rePasswordField);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("确定");            //注册流程
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						if (passwordField.getText().equals(rePasswordField.getText()))
						{
							User user=new User(usernameTextField.getText(),passwordField.getText());
							UserServiceImp userServiceImp = null;
							try {
								userServiceImp = new UserServiceImp();
								 if (userServiceImp.register(user)) JOptionPane.showMessageDialog(null, "successful");
							} catch (Exception e) {
								e.printStackTrace();
							}
						  }
						else JOptionPane.showMessageDialog(null, "no match");
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("取消");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
